<?php

/**
 * 案例设置类
 * author: heimuya
 * time: 2014-4-15
 */

namespace Admin\Controller;

use Think\Controller;

class ServiceController extends CommonController {

    public function _initialize() {
        parent::_initialize();
    }

    public function slist() {
        $NS = M('Report');
        $count = $NS->count();
        $Page = new \Think\Page($count, 10);
        $list = $NS->limit($Page->firstRow . ',' . $Page->listRows)->order('time desc')->select();
        $this->empty = "<td colspan='7' class='tb_heade'>暂无数据</td>";
        foreach ($list as $key => $value) {
            $list[$key]['title'] = (utf8_strlen($value['title']) > 8) ? utf_substr($value['title'], 16) . '...' : $value['title'];

            $list[$key]['description'] = (utf8_strlen($value['description']) > 12) ? utf_substr($value['description'], 24) . '...' : $value['description'];
        }
        $page = $Page->show();
        $this->assign("page", $page);
        $this->assign('list', $list);
        $this->display();
    }

    public function addservice() {
        $NS = M('Report');
        $list = $NS->field('id,name')->select();

        $this->assign('ctlist', $list);
        $this->display();
    }

    public function doaddservice() {
        if (I('title') == '') {
            $this->error("周报标题不能为空！");
            exit;
        }
        if (I('description') == '') {
            $this->error("周报简介不能为空！");
            exit;
        }
        $upload = new \Think\Upload(); // ;// 实例化上传类
        $upload->maxSize = 512000; // 设置附件上传大小 
        $upload->exts = array('jpg', 'gif', 'jpeg', 'png'); // 设置附件上传类型 
        $upload->autoSub = FALSE; // 设置附件上传类型
        $upload->savePath = './static/icon/'; // 设置附件上传目录 
        $info = $upload->upload();
        if ($info) {
            $temp_size = getimagesize(__ROOT__ . '/Uploads/static/icon/' . $info['image']['savename']);
            //dump($temp_size);
            if ($temp_size[0] > 200 || $temp_size[1] > 200) {
                $this->error('文件长度不要大于200px,宽度不要大于200px');
                exit;
            } else {
                $icon = __ROOT__ . '/Uploads/static/icon/' . $info['image']['savename'];
            }
        } else {
            $this->error("图标上传错误，请上传图标！");
            exit;
        }
        $data = array(
            'title' => I('title'),
            'description' => I('description'),
            'visible' => I('ifshow'),
            'content' => $_POST['content'],
            'icon' => $icon
        );
        $data['time'] = time();
        $data['content'] = htmlspecialchars($data['content']);
        //dump($data);die;
        $NS = M('Report');
        if ($rs = $NS->add($data)) {
            $this->success('周报增加成功');
            exit;
        } else {
            $this->success('周报增加失败，稍后重试');
            exit;
        }
    }
    
    public function scanservice() {
        $id = I('id', '', 'int');
        if ($id == '') {
            $this->error('参数错误');
        }
        $NC = M('Report');
        $report = $NC->where(array('id' => $id))->find();
        $this->assign('report', $report);
        $this->display();
    }

    public function editservice() {
        $id = I('id', '', 'int');
        if ($id == '') {
            $this->error('参数错误');
            exit;
        }
        $edit = M('Report')->where(array('id' => $id))->find();

        $this->assign('edit', $edit);
        $this->display();
    }

    public function doeditservice() {
        $id = I('id', '', 'int');
        if ($id == '') {
            $this->error('参数错误');
        }
        if (I('title') == '') {
            $this->error("周报标题不能为空！");
            exit;
        }
        if (I('description') == '') {
            $this->error("周报简介不能为空！");
            exit;
        }
        $data = array(
            'title' => I('title'),
            'description' => I('description'),
            'visible' => I('ifshow'),
            'content' => $_POST['content'],
        );
        $upload = new \Think\Upload(); // ;// 实例化上传类
        $upload->maxSize = 512000; // 设置附件上传大小 
        $upload->exts = array('jpg', 'gif', 'jpeg', 'png'); // 设置附件上传类型 
        $upload->autoSub = FALSE; // 设置附件上传类型
        $upload->savePath = './static/icon/'; // 设置附件上传目录 
        $info = $upload->upload();
        if ($info) {
            $temp_size = getimagesize(__ROOT__ . '/Uploads/static/icon/' . $info['image']['savename']);
            //dump($temp_size);
            if ($temp_size[0] > 200 || $temp_size[1] > 200) {
                $this->error('文件长度不要大于200px,宽度不要大于200px');
                exit;
            } else {
                $data['icon'] = __ROOT__ . '/Uploads/static/icon/' . $info['image']['savename'];
            }
        }
        $data['content'] = htmlspecialchars($data['content']);
        //dump($data);die;
        if ($rs = M('Report')->where(array('id' => $id))->setField($data)) {
            $this->success('周报编辑成功', U('service/slist'));
            exit;
        } else {
            $this->error('周报编辑失败，稍后重试');
            exit;
        }
    }

    public function delservice() {
        $id = I('id');
        if ($id == '') {
            $this->error('参数错误');
        }
        $rs = M('Report')->where(array('id' => $id))->delete();
        if ($rs !== false) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

}